<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 接口模块
 */
namespace app\platform\controller;

use  Mscore\Core\Controller;
use  Mscore\Core\Log;
use  Mscore\Core\Lib\Upload;
use  Mscore\Core\Lib\Mailer;
use  app\platform\model\Common as Common;
use  app\platform\model\Auth as Auth;
use  Mscore\Core\Lang;

class CommonController extends Controller{
   
    private $header;
    /**
     * 初始化自动连接方法
     * @param array __construct 
     * @param array language 
    */
    public function __construct()  
    {    
        (new Auth())->isAuth();
        $this->header['language']=isset($this->header['language'])&&!empty($this->header['language'])?$this->header['language']:'default';
        Lang::init($this->header['language']);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }

    /**
     * 接受业务系统上传的图片文件
     * @param array $getLang 
     * @param array $where 
    */
    public function acceptfile(){
      $param=post();
      $param['param']=json_decode($param['param'],true);
      $icon ='';
      $token=md5(config('file.acc').config('file.code'));
      $file = empty($param['param']['folder'])?'folder':$param['param']['folder'];
      try {
        $url='static/download/'.$file.'/';
        if(!is_dir(config('file.path').$url)){
          mkdir(config('file.path').$url, 0777, true); 
        }
        if(!isset($_FILES['file'])){
          return $this->echojson(Lang::get('Uploadempty'));
        }
        $icon = (new Upload)->Uploadfile($_FILES['file'],$url,$param['param']['filename']);
        Log::write(sprintf("upload sorce=【%s】,token=【%s】,file=【%s】", $file, $token, $icon),'upfile/',date("y_m_d").".log");
        $this->success['data']=['src'=>'/'.$icon];
        return $this->echojson($this->success);
      } catch (Exception $e) {
        Log::write(sprintf("upload sorce=【%s】,token=【%s】,file=【%s】", $file, $token, json_encode($e)),'upfile/',date("y_m_d").".log");
        return $this->echojson($this->error);
      }
    }
}